package com.clps.algorithm.chapter08.展平二叉搜索树;

import com.clps.algorithm.chapter07.二叉树每层的最大值.TreeNode;
import sun.reflect.generics.tree.Tree;

import java.util.Stack;

public class demo01 {
    public static void main(String[] args) {

    }

    public TreeNode increasingBST(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        TreeNode first = null;
        while (cur!=null ||!stack.isEmpty()){
            while (cur!=null){
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            if(prev!=null){
                prev.right = cur;
            }else{
                first = cur;
            }

            prev = cur;
            cur.left = null;
            cur = cur.right;
        }
        return  first;
    }
}


